/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.StorageDao) SALE.dao.local.StorageDao = function(){
	var storageDoc;
}

SALE.dao.local.StorageDao.prototype = {
		/**
		 * 
		 */
		initStorageDoc : function() {
			if(!this.storageDoc) {
				this.storageDoc = LocalXMLFileUtil.getStorageDoc();
			}
			
			return this.storageDoc;
		},
		
		saveStorageDoc : function() {
			LocalXMLFileUtil.saveXML(this.storageDoc,LocalXMLFileUtil.getStorageFile());
		},
		
		insertStorageInfo : function(storageJson) {
			var root = this.storageDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			storageJson["id"] = id;
			root.setAttribute("maxid",id);
			var storageNode = this.storageDoc.createElement("storage");
			
			this.updatePersonnel(storageJson, storageNode);			
			root.appendChild(storageNode);
			
			this.saveStorageDoc();
		},
		
		modifyStorageInfo : function(storageJson) {
			var storageNode = this.searchStorageByID(storageJson.id);
			
			this.updatePersonnel(storageJson, storageNode);
			
			this.saveStorageDoc();
		},
		
		deleteStorageInfo : function(id) {
			var storageNode = this.searchStorageByID(id);
			var root = this.storageDoc.documentElement;
			root.removeChild(storageNode);
		},
				
		updatePersonnel : function(storageJosn, storageNode) {
			
			for(var attri in storageJosn) {	
				
				if(attri.indexOf("old") != 0 && storageJosn[attri] && storageJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, storageNode);
					if(attriNodes != null && attriNodes.length > 0) {
						storageNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.storageDoc.createElement(attri);
					var contextNode = this.storageDoc.createCDATASection(storageJosn[attri]);
					attriNode.appendChild(contextNode);
					storageNode.appendChild(attriNode);
				}
			}
		},
		
		searchStorageByID : function(id) {
			var root = this.storageDoc.documentElement;
			var objArray = Ext.query("storage:has(id:nodeValue(" + id+ "))", root);
			var result;
						
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchStorageJsonByID : function(id) {
			
			var resultNode = this.searchStorageByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildStoragesJson : function() {
			var root = this.initStorageDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/storage", root);
				var storagesJson = {storages: []};
				
				Ext.each(objArray, function(storage) {
					var children = storage.childNodes;
					var storageJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!storageJson[child.nodeName] && child.childNodes[0] != null) {
								storageJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					storagesJson.storages.push(storageJson);
				});
				
				return storagesJson;
			}
		}
}